
const fs = require('fs')
const path = require('path')

module.exports = {

    fetch: function(req, res, {view = '', data = {}}) {
        view = (view == '') ? req.action : view;
        let controller = req.controller;
        if(view.indexOf('/') > 0) {
            
            let viewArr = view.split('/');
            controller = viewArr[0];
            view = viewArr[1];
        }
        let checkViewExists = fs.existsSync(path.join(__dirname, '/../../../views', `${req.moduleName}/${controller}/${view}.ejs`))
        if(!checkViewExists) {
            throw new Error(`View Engin Files [${req.moduleName}/${controller}/${view}] is not finde`)
        }
        res.render(`${req.moduleName}/${controller}/${view}`, data)
    }
}